package cn.cdeden.sys.utils.domain;

import cn.cdeden.sys.domain.dto.SysParametersConfigDto;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
//import com.cdyd.ecm.sys.domain.dto.SysParametersConfigDto;
//import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ProjectName : ecm_svn
 * @Package : com.cdyd.ecm.modules.synchronous
 * @ClassName : WeChatConfig
 * @Author : Yu.
 * @Description : // 企业微信配置
 * @Date : 2024/4/23 10:48
 * @Version : 1.0
 */
@Data
@Builder
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class WeChatConfig implements Serializable {
    /**
     * 序列化
     */
    private static final long serialVersionUID = 1L;

//    @ApiModelProperty(value = "程序Id")
    private String agentid;

//    @ApiModelProperty(value = "企业id")
    private String corpid;

//    @ApiModelProperty(value = "企业secret")
    private String corpsecret;

//    @ApiModelProperty(value = "是否开启状态")
    private boolean status;




    public  WeChatConfig( List<SysParametersConfigDto> infoList) {
        Map<String, SysParametersConfigDto> collect = infoList
                .stream()
                .collect(Collectors.toMap(SysParametersConfigDto::getConfigCode,
                        Function.identity()));
        SysParametersConfigDto status = collect.get("wx_login_status");
        SysParametersConfigDto corpsecret = collect.get("wx_corpsecret");
        SysParametersConfigDto agentid = collect.get("wx_agentid");
        SysParametersConfigDto corpid = collect.get("wx_corpid");

        this.status = Boolean.parseBoolean(status.getConfigValue());
        this.agentid = agentid.getConfigValue();
        this.corpid = corpid.getConfigValue();
        this.corpsecret = corpsecret.getConfigValue();
    }


    public void copyFrom(WeChatConfig source) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true));
    }



//    不可以为空的参数

//    public final String[] mandatory = new String[]{"wx_corpsecret","wx_agentid","wx_corpid"};


    public String getMandatory() {
        String info01 = "系统设置中集成配置,企业微信配置 `";
        StringJoiner stringJoiner = new StringJoiner(",");
        String info02 = "`信息不能为空";
        if (StringUtils.isBlank(this.agentid)){
            stringJoiner.add("wx_agentid");
        }
        if (StringUtils.isBlank(this.corpsecret)){
            stringJoiner.add("wx_corpsecret");
        }

        if (StringUtils.isBlank(this.corpid)){
            stringJoiner.add("wx_corpid");
        }

        int length = stringJoiner.length();

        // 判断长度
        return length > 0 ? info01 + stringJoiner + info02 : null;
    }




}
